// pages/store/index.js
import { PNT, setNavBarTitle, showToast, uniqueArray, pushPagePath } from "../../../utils/utils";
import { GetShopInfo, AddAttention,CheckAttention } from "../../../utils/apis";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    thumb:'https://m.360buyimg.com/mobilecms/s700x280_jfs/t1/84721/28/4434/139934/5de68864Edb1ea933/f0597ac2ca9f8143.jpg!cr_1125x445_0_171!q70.jpg.dpg',
    logo: 'https://x.dscmall.cn/storage/images/seller_imgs/seller_logo/logo_thumb/1536872208964241423.jpg',
    gzrs:10,
    num1:10,
    num2:5,
    num3:8,
    gz:1,
    prolist:[
      {
        thumb:'http://donglongshangshi.oss-cn-beijing.aliyuncs.com/images/shopfw/main/seller_id/3/2019/11/18/thumb_img/thumburl_G_1_521041867451_101.jpg',
        title:'Happy Monkey婴儿玩具动物风铃新生儿0-1岁床挂音乐玩具批发床铃',
        price:120,
      },
      {
        thumb: 'http://donglongshangshi.oss-cn-beijing.aliyuncs.com/images/shopfw/main/seller_id/3/2019/11/18/thumb_img/thumburl_G_1_521041867451_101.jpg',
        title: 'Happy Monkey婴儿玩',
        price: 120,
      },
      {
        thumb: 'http://donglongshangshi.oss-cn-beijing.aliyuncs.com/images/shopfw/main/seller_id/3/2019/11/18/thumb_img/thumburl_G_1_521041867451_101.jpg',
        title: 'Happy Monkey婴儿玩',
        price: 120,
      },
      {
        thumb: 'http://donglongshangshi.oss-cn-beijing.aliyuncs.com/images/shopfw/main/seller_id/3/2019/11/18/thumb_img/thumburl_G_1_521041867451_101.jpg',
        title: 'Happy Monkey婴儿玩',
        price: 120,
      },
      {
        thumb: 'http://donglongshangshi.oss-cn-beijing.aliyuncs.com/images/shopfw/main/seller_id/3/2019/11/18/thumb_img/thumburl_G_1_521041867451_101.jpg',
        title: 'Happy Monkey婴儿玩',
        price: 120,
      },
    ],
    page:0,
    flag:true,
    goods:[],
    cat:false,
    attention:0,
  },
  shopDetail(){
    wx.navigateTo({
      url: '/pages/store/storedetail/detail?ru_id='+this.data.ru_id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let ru_id = options.ru_id || 0;
    setNavBarTitle(PNT.goods.detail);
    this.loginModal = this.selectComponent("#login-modal");
    if(!ru_id){
      showToast('数据异常','warning');
      setTimeout(()=>{
        wx.navigateBack({
          delta:1,
        })
      },2000)
      return;
    }
    this.setData({
      ru_id:ru_id,
    })
    this.getShopInfo();
    this.checkAttention();

  },

  getShopInfo(){
    let data = {};
    let that = this;
    data.ru_id = this.data.ru_id;
    data.page = this.data.page;
    GetShopInfo(data).then(res=>{
      if(res.error_code == 10000){

        if(res.data.goods.length > 0){
          var list = this.data.goods;
          res.data.goods.map(res=>{
            list.push(res);
          })
          that.setData({
            flag: true,
            goods:list,
            data:res.data
          })
        }else{
          this.setData({
            data:res.data
          })
        }
      }else{
        showToast(res.msg,'warning');
      }
    })
  },
  checkAttention(){
    var data = {};
    data.ru_id = this.data.ru_id;
    if(!this.loginModal.check()){
      return false;
    }
    var info = wx.getStorageSync('userInfo');
    data.user_id = info.user_id;
    CheckAttention(data).then(res=>{
      if(res.error_code == 10000){
        this.setData({
          attention:res.data
        })
      }else{
        showToast(res.msg,'warning')
      }
    })
  },
  addAttention(){
    var data = {};
    data.ru_id = this.data.ru_id;
    if (!this.loginModal.check()) {
      return false;
    }
    var info = wx.getStorageSync('userInfo');
    data.user_id = info.user_id;
    AddAttention(data).then(res => {
      if (res.error_code == 10000) {
        this.setData({
          attention: res.data
        })
      } else {
        showToast(res.msg, 'warning')
      }
    })
  },
  

  goodsDetail(e){
    wx.navigateTo({
      url: '/pages/goods/detail/detail?id='+e.currentTarget.dataset.id,
    })
  },

  category(e){
    if(this.data.cat){
      this.setData({
        cat: false
      })
    }else{
      this.setData({
        cat: true
      })
    }

  },

    /**
   * 页面跳转
   * @author darlang
   */
  pushPath(e) {
    const items = e.currentTarget.dataset;
    const pathData = [
      { type: 'cat', path: '/pages/goods/list/list?catId=' + items.id+"&ru_id="+this.data.ru_id },
      { type: 'all', path: '/pages/goods/list/list?ru_id=' + this.data.ru_id },
      { type: 'new', path: '/pages/goods/list/list?is_new=1&ru_id=' + this.data.ru_id },
      { type: 'promote', path: '/pages/goods/list/list?is_promote=1&ru_id='+this.data.ru_id },
      { type: 'search', path: '/pages/goods/list/list?ru_id=' + this.data.ru_id },
    ];
    pushPagePath(e, pathData);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.flag) {
      wx.showLoading({ title: '加载中...', mask: true });
    }
    var page = this.data.page;
    page = page + 1;
    this.setData({
      page: page,
      flag: false,
    })
    this.getShopInfo();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})